package com.fy.common.util;

import lombok.extern.slf4j.Slf4j;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期格式化
 * @author huangming
 *
 */
@Slf4j
public class DateFormatUtil {
	public static String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
	public static String yyyy_MM_dd_HH_mm_ss_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
	public static String yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";
	public static String yyyy_MM_dd = "yyyy-MM-dd";
	public static String HH_mm = "HH:mm";
	public static String yyyyMMddHHmmss = "yyyyMMddHHmmss";
	public static String yyyyMMdd = "yyyyMMdd";
	public static String yyyyMM = "yyyyMM";
	public static String yyyy = "yyyy";
	public static String yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
	public static String yy = "yy";
	
	/***
	 * 取得格式化后的日期
	 * @return
	 */
	public static Date parseDate(String dateValue, String formatString){
		if (dateValue == null || formatString == null) {
			return null;
		}
		Date formatDate = null;
		try {
			if (yyyy_MM_dd_HH_mm_ss.equals(formatString)) {
				SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);
				sdf.setLenient(false);
				formatDate = sdf.parse(dateValue);
			} else if (yyyy_MM_dd_HH_mm.equals(formatString)){
				SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_HH_mm);
				sdf.setLenient(false);
				formatDate = sdf.parse(dateValue);
			} else if (yyyy_MM_dd.equals(formatString)) {
				SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd);
				sdf.setLenient(false);
				formatDate = sdf.parse(dateValue);
			} else if (HH_mm.equals(formatString)) {
				SimpleDateFormat sdf = new SimpleDateFormat(HH_mm);
				sdf.setLenient(false);
				formatDate = sdf.parse(dateValue);
			} else if (yyyyMMddHHmmss.equals(formatString)) {
				SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMddHHmmss);
				sdf.setLenient(false);
				formatDate = sdf.parse(dateValue);
			} else if (yyyyMMdd.equals(formatString)) {
				SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMdd);
				sdf.setLenient(false);
				formatDate = sdf.parse(dateValue);
			} else if (yyyy.equals(formatString)) {
				SimpleDateFormat sdf = new SimpleDateFormat(yyyy);
				sdf.setLenient(false);
				formatDate = sdf.parse(dateValue);
			} else if (yyyyMMddHHmmssSSS.equals(formatString)) {
				SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMddHHmmssSSS);
				sdf.setLenient(false);
				formatDate = sdf.parse(dateValue);
			} else if (yyyyMM.equals(formatString)) {
				SimpleDateFormat sdf = new SimpleDateFormat(yyyyMM);
				sdf.setLenient(false);
				formatDate = sdf.parse(dateValue);
			} else if (yyyy_MM_dd_HH_mm_ss_SSS.equals(formatString)) {
				SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss_SSS);
				sdf.setLenient(false);
				formatDate = sdf.parse(dateValue);
			} else if (yy.equals(formatString)) {
				SimpleDateFormat sdf = new SimpleDateFormat(yy);
				sdf.setLenient(false);
				formatDate = sdf.parse(dateValue);
			}
		} catch (ParseException e) {
			log.error("日期格式化错误：DateFormatUtil error");
			e.printStackTrace();
		}

		return formatDate;
	}
	
	/**
	 * 取得格式化日期字符串
	 * @return
	 */
	public static String formatDate(Date dateValue, String formatString){
		if (dateValue == null || formatString == null) {
			return null;
		}
		String formatDateString = null;
		if (yyyy_MM_dd_HH_mm_ss.equals(formatString)) {
			SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);
			formatDateString = sdf.format(dateValue);
		} else if (yyyy_MM_dd_HH_mm.equals(formatString)) {
			SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_HH_mm);
			formatDateString = sdf.format(dateValue);
		} else if (yyyy_MM_dd.equals(formatString)) {
			SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd);
			formatDateString = sdf.format(dateValue);
		} else if (HH_mm.equals(formatString)) {
			SimpleDateFormat sdf = new SimpleDateFormat(HH_mm);
			formatDateString = sdf.format(dateValue);
		} else if (yyyyMMddHHmmss.equals(formatString)) {
			SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMddHHmmss);
			formatDateString = sdf.format(dateValue);
		} else if (yyyyMMdd.equals(formatString)) {
			SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMdd);
			formatDateString = sdf.format(dateValue);
		} else if (yyyy.equals(formatString)) {
			SimpleDateFormat sdf = new SimpleDateFormat(yyyy);
			formatDateString = sdf.format(dateValue);
		} else if (yyyyMMddHHmmssSSS.equals(formatString)) {
			SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMddHHmmssSSS);
			formatDateString = sdf.format(dateValue);
		} else if (yyyyMM.equals(formatString)) {
			SimpleDateFormat sdf = new SimpleDateFormat(yyyyMM);
			formatDateString = sdf.format(dateValue);
		} else if (yyyy_MM_dd_HH_mm_ss_SSS.equals(formatString)) {
			SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss_SSS);
			formatDateString = sdf.format(dateValue);
		} else if (yy.equals(formatString)) {
			SimpleDateFormat sdf = new SimpleDateFormat(yy);
			formatDateString = sdf.format(dateValue);
		}
		return formatDateString;
	}
	
	public static String formatDateStringLength(String dateString, String formatString){
		String retValue  = null;
		if(dateString!=null){
			if(dateString.length()>formatString.length()){
				retValue = dateString.substring(0, formatString.length());
			}else{
				retValue = dateString;
			}
		}
		return retValue;
	}

	/**
	 * 日期比较
	 * @param DATE1
	 * @param DATE2
	 * @return
	 */
	public static int compareDate(String DATE1, String DATE2) {


		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				System.out.println("dt1 在dt2前");
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				System.out.println("dt1在dt2后");
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 获取一个时间与服务器时间的时间差，返回单位秒
	 * @param startDate
	 * @return
	 */
	public static int calLastedTime(Date startDate) {
		long a = System.currentTimeMillis();
		long b = startDate.getTime();
		int c = (int)((a - b) / 1000);
		return c;
	}

	/**
	 * 获取距离基础时间（1970年1月1日0时0分0秒）开始的秒数
	 * @param other
	 *          yyyy-MM-dd HH:mm:ss
	 * @return
	 *          毫秒（ 1 秒  = 1000毫秒）
	 */
	public static long getSecondAwayFromBaseTime(String other){

		Timestamp otherTimeSpan = Timestamp.valueOf(other);

		long seconds = otherTimeSpan.getTime()/1000;

		return seconds;
	}

	/**
	 * 获取距离基础时间（1970年1月1日0时0分0秒）开始的毫秒数
	 * @param other
	 * @return
	 */
	public static long getTimeMillisAwayFromBaseTime(String other){

		Timestamp otherTimeSpan = Timestamp.valueOf(other);

		return otherTimeSpan.getTime();
	}

	/**
	 * 时间戳转换成日期格式字符串
	 * @param seconds 精确到秒的字符串
	 * @param formatStr
	 * @return
	 */
	public static String timeStamp2Date(String seconds,String formatStr) {
		if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
			return "";
		}
		if(formatStr == null || formatStr.isEmpty()){
			formatStr = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		return sdf.format(new Date(Long.valueOf(seconds+"000")));
	}
	/**
	 * 日期格式字符串转换成时间戳
	 * @param date_str 字符串日期
	 * @param format 如：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String date2TimeStamp(String date_str,String format){
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return String.valueOf(sdf.parse(date_str).getTime()/1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}
